package com.dtsz.autoconfigure.framework.redis.utils;

import lombok.NoArgsConstructor;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.target.SingletonTargetSource;

import java.util.Objects;

import static lombok.AccessLevel.PRIVATE;

@NoArgsConstructor(access = PRIVATE)
public class ProxyUtil {

    /**
     * 通过代理对象获取真实的类
     *
     * @param proxyObject 代理对象
     * @return 真实类
     */
    public static Class<?> getOriginClass(Object proxyObject) {
        if (Objects.nonNull(proxyObject)) {
            if (proxyObject instanceof Advised) {
                Advised advised = (Advised) proxyObject;
                Object target = ((SingletonTargetSource) (advised).getTargetSource()).getTarget();
                if (Objects.nonNull(target)) {
                    return target.getClass();
                }
            } else {
                return proxyObject.getClass();
            }
        }
        return null;
    }
}
